<template>
  <div class="potentialCustomers">
    <page-header :title="'交易管理'"></page-header>
    <div class="search_area">
      <el-row>
        <el-form :form="queryForm" label-width="1rem">
          <el-col :span="8">
            <el-form-item label="交易名称">
              <el-input type="text" v-model="queryForm.tranName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户名称">
              <el-input type="text" v-model="queryForm.customerName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人">
              <el-input type="text" v-model="queryForm.contact"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="交易阶段">
              <el-select v-model="queryForm.stage" clearable placeholder="请选择" style="width:100%">
                <el-option v-for="item in stage" :label="item.described" :value="item.code" :key="item.code"></el-option>
              </el-select>
              
            </el-form-item>
          </el-col>
        </el-form>
        <el-col :span="24">
          <div class="btn_area">
            <el-button class="confrim" @click="queryList">查询</el-button>
            <el-button class="cancle" @click="reset">重置</el-button>
            <el-button class="confrim" @click="add">新增</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <table-list :fields="fields" :list="list" :pagination="pagination" :total="total" @sizeChange="handleSizeChange" @currentChange="handleCurrentChange" @handle="onHandle" :loading="loading"></table-list>
    <el-dialog :title="code == '1' ? '新增交易': '编辑交易'" :visible.sync="visible" @close="close" width="65%">
      <el-row>
        <el-form :form="tranForm" label-width="1.5rem">
          <el-col :span="11">
            <el-form-item label="交易名称" required>
              <el-input v-model="tranForm.tranName" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="交易客户" required>
              <el-select :disabled="code != '1'" v-model="tranForm.customerId" clearable placeholder="请选择" style="width:100%" @change="customChange" >
                <el-option v-for="item in custom" :label="item.companyName" :value="item.uuid" :key="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="联系人" required>
              <el-select v-model="tranForm.contactId" clearable placeholder="请选择" style="width:100%">
                <el-option v-for="item in contactList" :label="item.contactName" :value="item.uuid" :key="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="交易阶段" required>
              <el-select v-model="tranForm.stage" clearable placeholder="请选择" style="width:100%">
                <el-option v-for="item in stage" :label="item.described" :value="item.code" :key="item.code"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="交易金额" required>
              <el-input v-model="tranForm.money" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="所有者" required>
              <el-select v-model="tranForm.owner" clearable placeholder="请选择" style="width:100%">
                <el-option v-for="item in ownerList" :label="item.userName" :value="item.uuid" :key="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="联系纪要" required>
              <el-input type="textarea" v-model="tranForm.contactNotes" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="描述" required>
              <el-input type="textarea" v-model="tranForm.described" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="创建人">
               <el-input disabled v-model="tranForm.createByName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="创建时间">
              <el-input disabled v-model="tranForm.createTime"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="更新人">
              <el-input disabled v-model="tranForm.updateByName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="更新时间">
              <el-input disabled v-model="tranForm.updateTime"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancle" @click="visible = false">取 消</el-button>
        <el-button class="confrim" @click="confrim">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import tableList from '@/components/table.vue'
import pageHeader from '@/components/pageHeader.vue'
export default {
  name: 'potentialCustomers',
  components: {
    tableList,
    pageHeader
  },
  data() {
    return {
      loading: false,
      queryForm: {
        tranName: '',
        customerName: '',
        contact : '',
        stage: ''
      },
      list: [],
      fields: [
        {
          prop: 'rowNum',
          label: '序号',
          align: 'center'
        },
        {
          prop: 'tranName',
          label: '交易名称',
          align: 'center'
        },
        {
          prop: 'customerName',
          label: '客户名称',
          align: 'center'
        },
        {
          prop: 'contact',
          label: '联系人',
          align: 'center'
        },
        {
          prop: 'stageName',
          label: '交易阶段',
          align: 'center'
        },
        {
          prop: 'ownerName',
          label: '所有者',
          align: 'center'
        },
        {
          prop: 'createByName',
          label: '创建人',
          align: 'center'
        },
        {
          prop: 'oper',
          label: '操作',
          align: 'center',
          btns: [
            {type: 'update',desc:'编辑'}
          ]
        }
      ],
      pagination: {
        currentPage: 1,
        pageSize: 10
      },
      total: 0,
      stage: [],
      visible: false,
      code: '',
      tranForm: {
        adminId: "",
        contactId: "",
        contactNotes: "",
        createBy: "",
        createByName: "",
        createTime: "",
        customerId: "",
        described: "",
        money: "",
        owner: "",
        stage: "",
        tranName: "",
        updateBy: "",
        updateByName: "",
        updateTime: "",
		    uuid: ""
      },
      ownerList: [],
      custom: [],
      contactList: [],
      userData: {},
    }
  },
  methods: {
    reset() {
      this.queryForm.tranName = '',
      this.queryForm.customerName = '',
      this.queryForm.contact = '',
      this.queryForm.stage = ''
    },
    add() {
      this.code = '1';
      this.visible = true;
      this.tranForm.adminId = this.userData.uuid;
      this.tranForm.createBy = this.userData.uuid;
      this.tranForm.createTime = this.$time();
    },
    close() {
      this.contactList = [];
      this.tranForm = {
        adminId: "",
        contactId: "",
        contactNotes: "",
        createBy: "",
        createByName: "",
        createTime: "",
        customerId: "",
        described: "",
        money: "",
        owner: "",
        stage: "",
        tranName: "",
        updateBy: "",
        updateByName: "",
        updateTime: "",
		    uuid: ""
      }
    },
    confrim() {
      let url = this.code == '1' ? this.$api.tran.create : this.$api.tran.update
      this.$post(url,this.reqData(this.tranForm)).then(res => {
        if (res.data.code === 200) {
          this.$message.success(res.data.message);
          this.visible = false;
          this.queryList()
        } else {
          this.$message.error(res.data.message);
        }
      })
    },
    customChange(val) {
      this.tranForm.contactId = "";
      this.contactList = [];
      this.$post(this.$api.custom.detail,this.reqData(val)).then(res => {
        if (res.data.code == 200) {
          let data = res.data.data;
          this.contactList = data.contactList
        }
      })
    },
    onHandle(data) {
      let {row,type} = data;
      this.code == '2'
      this.visible = true;
      this.$post(this.$api.tran.info,this.reqData(row.uuid)).then(res => {
        if (res.data.code == 200) {
          let data = res.data.data;
          this.$post(this.$api.custom.detail,this.reqData(data.customerId)).then(res => {
            if (res.data.code == 200) {
              let data = res.data.data;
              this.contactList = data.contactList
            }
          })
          this.tranForm = data
          this.tranForm.adminId = this.userData.uuid;
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    queryList() {
      this.loading = true;
      this.$post(this.$api.tran.list,this.reqData(this.queryForm,this.pagination.currentPage,this.pagination.pageSize)).then(res => {
        if (res.data.code === 200) {
          let data = res.data.data
          this.list = data.list;
          this.list.forEach((item,index) => {
            item.rowNum = (this.pagination.currentPage - 1) * this.pagination.pageSize + index + 1;
          })
          this.loading = false
          this.total = data.total;
        } else {
          this.$message.error(res.data.message)
          this.loading = false
        }
      })
    },
    handleSizeChange(val) {
      console.log(val);
      this.pagination.pageSize = val;
      this.queryList()
    },
    handleCurrentChange(val) {
      console.log(val);
      this.pagination.currentPage = val;
      this.queryList()
    },
    queryCode() {
      this.$post(this.$api.common.code,this.reqData('stage')).then(res => {
        if (res.data.code === 200) {
          this.stage = res.data.data;
        }
      })
    },
    queryUserList() {
      let url = this.$api.user.list;
      this.$post(url).then(res => {
        if (res.data.code == 200) {
          this.ownerList = res.data.data;
        }
      })
    },
    queryCustom() {
      this.$post(this.$api.custom.list,this.reqData(this.queryForm,1,9999)).then(res => {
        this.loading = false
        if (res.data.code === 200) {
          let data = res.data.data
          this.custom = data.list;
        }
      })
    },
  },
  mounted() {
    this.queryUserList();
    this.queryCode();
    this.queryCustom();
    this.userData = JSON.parse(sessionStorage.getItem('userData'))
    this.queryList()
  }
}
</script>
<style scoped>
.potentialCustomers {
  background: #fff;
  overflow: hidden;
  padding-bottom: 0.15rem;
}
</style>